# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name：     NewLoguru.py
   Description :
   Author :        ZhangYiLin
   date：          2020/12/14 14:40
-------------------------------------------------
   Change Activity:
                   2020/12/14 14:40
-------------------------------------------------
"""
import os
from config.ConfUi import ROOT_DIR
from loguru import logger


class LogConfig:
    """
    日志
    """

    def __init__(self):
        log_path = os.path.join(ROOT_DIR, 'logs')
        logger.add(  # 错误日志
            os.path.join(log_path, 'Error/{time:YYYY-MM-DD}.log'),
            format="{time:YYYY-MM-DD at HH:mm:ss} | {level} | {module}:{function}:{line} | {message}",
            filter=lambda x: True if x['level'].name == 'ERROR' else False,
            rotation="00", retention=7, level='ERROR', encoding='utf-8'
        )
        logger.add(  # 成功日志
            os.path.join(log_path, 'Success/{time:YYYY-MM-DD}.log'),
            format="{time:YYYY-MM-DD at HH:mm:ss} | {level} | {module}:{function}:{line} | {message}",
            filter=lambda x: True if x['level'].name == 'SUCCESS' else False,
            rotation="00", retention=7, level='SUCCESS', encoding='utf-8'
        )
        logger.add(  # Default日志
            os.path.join(log_path, 'Default/{time:YYYY-MM-DD}.log'),
            format="{time:YYYY-MM-DD at HH:mm:ss} | {level} | {module}:{function}:{line} | {message}",
            rotation="00", retention=7, level='DEBUG', encoding='utf-8'
        )

        self.logger = logger

    def get(self):
        """
        返回logger对象
        """
        return self.logger


global_log = LogConfig().get()


if __name__ == '__main__':
    global_log.error('123')
    global_log.info('123')
    global_log.warning('123')
